package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.QuestionItem;
import com.itheima.domain.Result;
import com.itheima.QuestionItemService;
import com.itheima.utils.BeanUtil;
import com.itheima.web.filters.BeanFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * http://localhost:8080/heima_mm/store/questionItem/findByPage?currentPage=1&Size=5
 * 可以接受来自store/questionItem下面多有的方法
 */
@WebServlet("/store/questionItem/*")
public class QuestionItemServlet extends BaseServlet {
private QuestionItemService questionItemService= BeanFactory.getBean("questionItemService",QuestionItemService.class);

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
//        获取请求参数并封装成QuestionItem对象
        try {
            QuestionItem questionItem = BeanUtil.fillBeanFromJson(request, QuestionItem.class);
//            调用service层方法添加数据
            questionItemService.save(questionItem);
//        响应结果
            return new Result(true,MessageConstant.SAVE_COMPANY_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_COMPANY_FAIL);
        }

    }
    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
//        获得请求参数
            String questionItemId = request.getParameter("id");
//            调用Service层方法
            QuestionItem questionItem = questionItemService.findById(questionItemId);
//       对客户做出响应
            return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS,questionItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_COMPANY_FAIL);
        }

    }
    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
//        获取请求参数
        try {
            QuestionItem questionItem = BeanUtil.fillBeanFromJson(request, QuestionItem.class);
//       调用serice层方法，修改数据
            questionItemService.update(questionItem);
//        响应结果
            return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_COMPANY_FAIL);
        }
    }
    /**
     * 处理删除数据的方法
     * @param request
     * @param response
     */
    private Result delete(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
        try {
            //1 获取请求参数
            String id = request.getParameter("id");
            //2 调用service层方法，删除数据
            questionItemService.delete(id);
            //3 响应结果
            return new Result(true, MessageConstant.DELETE_QUESTIONITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_QUESTIONITEM_FAIL);
        }
    }
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1 获取问题id
            String questionId = request.getParameter("questionId");
            //2 调用service层方法获取分页结果
            List<QuestionItem> list = questionItemService.findAll(questionId);
            //3 对客户的作出响应
            return new Result(true, MessageConstant.QUERY_QUESTIONITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_QUESTIONITEM_FAIL);
        }
    }
}